package com.harzone.commons.search.impl.statement;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.common.SolrInputDocument;

import com.harzone.commons.search.domain.Field;
import com.harzone.commons.search.domain.Record;
import com.harzone.commons.search.statement.Update;
import com.harzone.commons.search.visitor.RecordVisitor;
import com.harzone.commons.search.visitor.UpdateVisitor;

public class UpdateParser extends BaseParser implements RecordVisitor, UpdateVisitor {

	private Collection<SolrInputDocument> docs;

	public UpdateParser(Update update) {
		super();
		this.visit(update);
	}

	@Override
	public void visit(Update update) {
		this.docs = new ArrayList<SolrInputDocument>(update.getRecords().size());
		for (Record record : update.getRecords()) {
			this.visit(record);
		}
		WhereParser w = new WhereParser(update.getWhere());
		w.parse(query);
	}

	@Override
	public void visit(Record record) {
		SolrInputDocument doc = new SolrInputDocument();
		List<Field> fields = record.getFields();
		for (Field field : fields) {
			doc.addField(field.getIndexName(), field.getValue(), field.getBoost());
		}
		docs.add(doc);
	}


	public Collection<SolrInputDocument> getDocs() {
		return docs;
	}

}
